package com.zs.pig.common.filter;

import java.io.IOException;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.zs.pig.common.redis.test.RedisUtils;
import com.zs.pig.common.sys.model.SysUser;
import com.zs.pig.common.tools.SnowflakeIdWorker;
import com.zs.pig.common.utils.IPUtils;
import com.zs.pig.common.utils.JsonUtils;
import com.zs.pig.log.api.model.PlogWithBLOBs;
import com.zs.pig.web.sys.utils.SysUserUtils;

/**
 * 记录访问日志
 */

public class AccessLogFilter extends BaseFilter {

	 
	 // PlogService PlogService = SpringContextUtil.getBean("PlogService");
	  RedisUtils  RedisUtils=new RedisUtils(6);
	  SnowflakeIdWorker idWorker = new SnowflakeIdWorker(0, 0);
	@Override
	public void doFilter(HttpServletRequest request,
			HttpServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		//LogUtils.logAccess(request);
		 String username = getUsername();
	        String jsessionId = request.getRequestedSessionId();
	        String ip = IPUtils.getClientAddress(request);
	        String accept = request.getHeader("accept");
	        String userAgent = request.getHeader("User-Agent");
	        String url = request.getRequestURI();
	        String params = getParams(request);
	        String headers = getHeaders(request);

	    	PlogWithBLOBs log = new PlogWithBLOBs();
	        log.setRemoteAddr(IPUtils.getClientAddress(request));
			log.setRequestUri(request.getRequestURI());
			log.setMethod(request.getMethod());
			log.setUserAgent(request.getHeader("user-agent"));
			log.setCreateBy(username);
			log.setCreateDate(new Date());
			Random r=new Random();
			log.setType(r.nextInt(10));
			RedisUtils.hset(username, idWorker.nextId()+"", JsonUtils.objectToJson(log));
			//log保存到数据库
		//	PlogService.savePlog(log);
		chain.doFilter(request, response);
	}
	  public static String getBlock(Object msg) {
	        if (msg == null) {
	            msg = "";
	        }
	        return "[" + msg.toString() + "]";
	    }



		protected static String getParams(HttpServletRequest request) {
	        Map<String, String[]> params = request.getParameterMap();
	        return JSON.toJSONString(params);
	    }


		private static String getHeaders(HttpServletRequest request) {
	        Map<String, List<String>> headers = Maps.newHashMap();
	        Enumeration<String> namesEnumeration = request.getHeaderNames();
	        while(namesEnumeration.hasMoreElements()) {
	            String name = namesEnumeration.nextElement();
	            Enumeration<String> valueEnumeration = request.getHeaders(name);
	            List<String> values = Lists.newArrayList();
	            while(valueEnumeration.hasMoreElements()) {
	                values.add(valueEnumeration.nextElement());
	            }
	            headers.put(name, values);
	        }
	        return JSON.toJSONString(headers);
	    }


	    protected static String getUsername() {
	    	SysUser sysUser = SysUserUtils.getCacheLoginUser();
	    	if(sysUser == null) return "notLogin";
	        return sysUser.getName();
	    }
}
